﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class PlayScreen : GameScreen
    {
#region Properties
        private PlayBGComponent _playCharacterComponent;
        private PlayConstComponent _playConstComponent;
        private PlayUpdateComponent _playUpdateComponent;
        private PlayPauseComponent _playPauseComponent;
        private PlayShopComponent _playShopComponent;
        private BehindPlayerComponent _behindPlayerComponent;
        private FrontPlayerComponent _frontPlayerComponent;
        private bool isPause = false;
        private bool isShop = false;
        private long tempLuffyBalance;
#endregion

#region  Initilation
        public PlayScreen(Game game, MainGame mainGame, SpriteBatch spriteBatch, SpriteFont textFont, SpriteFont menuFont, List<Texture2D> listBG, List<Texture2D> ViewIcon, List<Texture2D> listLayerBehind,List<Texture2D> listLayerFront)
            : base(game, mainGame, spriteBatch)
        {
            _game = game;
            _mainGame = mainGame;
            _spriteBatch = spriteBatch;

            //Init Menucomponent
            //BehindLayer lat vat ve dau tien
            _behindPlayerComponent = new BehindPlayerComponent(game,spriteBatch,listLayerBehind);
            Components.Add(_behindPlayerComponent);
            // Character
            _playCharacterComponent = new PlayBGComponent(_game, _spriteBatch) { Parallax = Vector2.One };
            Components.Add(_playCharacterComponent);
            _playConstComponent = new PlayConstComponent(_game, _spriteBatch, ViewIcon) { Parallax = Vector2.Zero };
            // FrontLayer 
            _frontPlayerComponent = new FrontPlayerComponent(game, spriteBatch, listLayerFront);
            Components.Add(_frontPlayerComponent);
            // Control
            Components.Add(_playConstComponent);
            _playUpdateComponent = new PlayUpdateComponent(_game, _mainGame, _spriteBatch, ViewIcon[6],ViewIcon[5]);
                _playConstComponent.Pause.Selected += new Button.IsSelected(Pause_Selected);                
            //Only for Update
            Components.Add(_playUpdateComponent);
            _playPauseComponent = new PlayPauseComponent(_game, _spriteBatch,listBG[7], textFont);
                _playPauseComponent.Resume.Selected += new Button.IsSelected(Resume_Selected);
                _playPauseComponent.Quit.Selected += new Button.IsSelected(Quit_Selected);
            _playShopComponent = new PlayShopComponent(_game, _spriteBatch, listBG[9], textFont, menuFont);
                _playShopComponent.Back.Selected += new Button.IsSelected(ShopBack_Selected);
                ManagerGame.goToShop.Selected += new Button.IsSelected(Shop_Selected);
        }
        
            
#endregion

#region Methods
        public override void  ResetScreen(int input)
        {
 	        base.ResetScreen(input);
            isPause = false;
            ManagerGame.managerAHero.SetAHeroIsSelect(ManagerGame.luffy.LisAHeronow);
            ManagerGame.managerZombie.ResetAtLevelScreen(ManagerGame.luffy.LevelScreen);
            ManagerGame.managerTrap.ResetTrap();
            ManagerGame.treasure.Reset();
            tempLuffyBalance = ManagerGame.luffy.Balance;
            if (ManagerGame.SoundOn)
                MediaPlayer.Play(ManagerGame.managerSound._gamePlay);
        }

        void Shop_Selected(int input)
        {
            // Lam gi de goi shop
            isShop = true;
        }

        void ShopBack_Selected(int input)
        {
            isShop = false;
        }

        void Pause_Selected(int input)
        {
            isPause = true;
        }

        void Resume_Selected(int input)
        {
            isPause = false;
        }

        void Quit_Selected(int input)
        {
            MediaPlayer.Stop();
            ManagerGame.luffy.Balance = tempLuffyBalance;
            ManagerGame.luffy.ResetLuffy();
            ManagerGame.camera.LookAt(ManagerGame.luffy.Position);
            _mainGame.ShowScreen(0);
        }
#endregion

#region Update & Draw
        public override void Update(GameTime gameTime)
        {
            if (isPause)
                _playPauseComponent.Update(gameTime);
            else if (isShop)
                _playShopComponent.Update(gameTime);
            else
                base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (isShop)
                _playShopComponent.Draw(gameTime);
            else
                base.Draw(gameTime);
            if (isPause)
                _playPauseComponent.Draw(gameTime);
        }
#endregion
    }
}
